

class DataParser:
    def __init__(self,evOneFrame):
        self.buffer_i = [0] * 100

        self.readingIndex = -1
        self.check = 0
        self.cmd = 0

        self.oneframeEvent = evOneFrame

    def DecodeByBytes(self, data_bytes):
        """
        字节串解析（注意不是字节数组）
        :param data_bytes: 待解析的字节串
        """
        data = bytearray(data_bytes)
        for b in data:
            self.DecodeByOneByte(b)

    def DecodeByOneByte(self,onebyte):
        """
        一个字节解析
        :param onebyte: 一个字节
        """
        rdata_i = onebyte
        if not isinstance(onebyte,int):
            rdata_i = int.from_bytes(onebyte, 'big')


        if rdata_i == 36 :
            print('one frame')
            self.readingIndex = 0
            self.check=0
        elif self.readingIndex == 1:
            self.cmd = rdata_i
        elif 2 < self.readingIndex <= (self.buffer_i[2] + 2):
            self.check = self.check ^ rdata_i
        elif (2 < self.readingIndex <= self.buffer_i[2] + 6 and self.buffer_i[self.readingIndex] == 10
              and self.buffer_i[self.readingIndex - 1] == 13):
            self.readingIndex = -1
            # TODO 一帧数据结束，显示结果...
            #//TODO 异或操作 。。。if self.check==self.buffer_i[self.buffer_i[2] + 3]:
            if self.cmd == 1:
                # 姿态信息...
                print('#OneFrameOk.')
                if self.oneframeEvent is not None:
                    self.oneframeEvent(self.buffer_i)

        self.buffer_i[self.readingIndex] = rdata_i
        self.readingIndex += 1